/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BedPanel.java
 *
 * Created on 17 Feb 2011, 3:48:00 PM
 */
package za.co.pas.spaceandtime.view.panel;

import za.co.pas.spaceandtime.data.Bed;
import za.co.pas.spaceandtime.data.BedType;
import za.co.pas.spaceandtime.model.combo.BedTypeComboModel;

/**
 *
 * @author alabuschagne
 */
public class BedPanel extends javax.swing.JPanel
{
    private Bed currentBed;
    private BedTypeComboModel bedTypeComboModel = new BedTypeComboModel();

    /** Creates new form BedPanel */
    public BedPanel()
    {
        initComponents();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        companyNamePanel = new javax.swing.JPanel();
        maxCapSpinner = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        enabledCheckBox = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        roomTypeComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        companyNamePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Name", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        maxCapSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

        jLabel3.setText("Max capacity:");

        enabledCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        jLabel4.setText("Enabled:");

        roomTypeComboBox.setModel(bedTypeComboModel);

        jLabel6.setText("Bed type:");

        javax.swing.GroupLayout companyNamePanelLayout = new javax.swing.GroupLayout(companyNamePanel);
        companyNamePanel.setLayout(companyNamePanelLayout);
        companyNamePanelLayout.setHorizontalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(31, 31, 31)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(enabledCheckBox)
                    .addComponent(maxCapSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(roomTypeComboBox, 0, 252, Short.MAX_VALUE))
                .addContainerGap())
        );
        companyNamePanelLayout.setVerticalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(roomTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxCapSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(enabledCheckBox))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companyNamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companyNamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel companyNamePanel;
    private javax.swing.JCheckBox enabledCheckBox;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSpinner maxCapSpinner;
    private javax.swing.JComboBox roomTypeComboBox;
    // End of variables declaration//GEN-END:variables

    public void setBed(Bed bed)
    {
        currentBed = bed;
        updateFields();
    }

    public Bed getBed()
    {
        updateData();
        return currentBed;
    }

    private void updateFields()
    {
        if(currentBed != null)
        {
            this.bedTypeComboModel.setSelectedItem(currentBed.getBedType());
            this.enabledCheckBox.setSelected(currentBed.getEnabled());
            this.maxCapSpinner.setValue(currentBed.getMaxCapacity());
        }
    }

    private void updateData()
    {
        if(currentBed == null)
        {
            currentBed = new Bed();
        }
        currentBed.setBedType((BedType) this.bedTypeComboModel.getSelectedItem());
        currentBed.setEnabled(this.enabledCheckBox.isSelected());
        currentBed.setMaxCapacity((Integer) this.maxCapSpinner.getValue());
    }
}
